using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.Common;
using Garlic.CC.Utility;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

using Garlic.DM.DataModel;

namespace Garlic.DAL
{
	/// <summary>
	/// DataAccess
	/// </summary>
	public partial class SysConfigDAL
	{
		#region Sql const
		public  const string TS_BAS_SYSCONFIG_SELECT_BY_ID=" SELECT parameter_value,parameter_description,default_value,changeable,parameter_name"+
											" FROM TS_BAS_SysConfig"+
											" WHERE 1=1 " +
											" and  parameter_name =  :parameter_name";

											
		public  const string TS_BAS_SYSCONFIG_SELECT=" SELECT "+
											"parameter_value,"+
											"parameter_description,"+
											"default_value,"+
											"changeable,"+
											"parameter_name"+
											" FROM TS_BAS_SysConfig {0}";
														
								
	
		public  const string TS_BAS_SYSCONFIG_INSERT=" INSERT INTO TS_BAS_SysConfig ("+
											"parameter_value,"+
											"parameter_description,"+
											"default_value,"+
											"changeable,"+
											"parameter_name"+
										") VALUES ("+
											":parameter_value,"+
											":parameter_description,"+
											":default_value,"+
											":changeable,"+
											":parameter_name"+
										")";
									
									
		public  const string TS_BAS_SYSCONFIG_UPDATE=" UPDATE TS_BAS_SysConfig SET "+
											" parameter_value =  :parameter_value, "+
											" parameter_description =  :parameter_description, "+
											" default_value =  :default_value, "+
											" changeable =  :changeable "+
										" WHERE 1=1 " +
											" and  parameter_name =  :parameter_name";
										
	
	
		public  const string TS_BAS_SYSCONFIG_DELETE=" DELETE FROM TS_BAS_SysConfig" +
										" WHERE 1=1"+
											" and  parameter_name =  :parameter_name";
										
										
		
		#endregion
		
		/// <summary>
		/// Get SysConfigInfo by parameter_name
		/// </summary>
		/// <param name="parameter_name">SysConfigInfo </param>
		/// <returns></returns>
		public static SysConfigInfo GetInfo(string parameter_name)
		{
	
				Database db = DatabaseFactory.CreateDatabase();
				DbCommand dbCommand = db.GetSqlStringCommand(TS_BAS_SYSCONFIG_SELECT_BY_ID);
			db.AddInParameter(dbCommand, "parameter_name", DbType.AnsiString,parameter_name);
				

				using (IDataReader dr = db.ExecuteReader(dbCommand))
				{
					if (dr.Read())
					{
						return CreateSysConfigInfo(dr);
					}
					else
						return null;
				}
		}
	
	
		/// <summary>
		/// Get data list
		/// </summary>
		/// <param name="textWhere">conditon</param>
		/// <param name="orderText">sort</param>
		/// <returns>SysConfigInfo list </returns>
		public static List<SysConfigInfo> GetList(string textWhere,string orderText)
		{
			string query=string.Empty;
			if(!string.IsNullOrEmpty(textWhere))
			{
			query+=" where "+textWhere; 
			}
			
			if(!string.IsNullOrEmpty(orderText))
			{
			query+=" order by "+orderText;
			}
			
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand dbCommand = db.GetSqlStringCommand(string.Format(TS_BAS_SYSCONFIG_SELECT,query));
			
			return GetList(string.Format(TS_BAS_SYSCONFIG_SELECT,query));
			
		}
		
		/// <summary>
		/// Get data list
		/// </summary>
		/// <param name="sql">SQL statement</param>
		/// <returns>SysConfigInfo list </returns>
		public static List<SysConfigInfo> GetList(string sql)
		{
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand dbCommand = db.GetSqlStringCommand(sql);
			List<SysConfigInfo> list=new List<SysConfigInfo>();
			using (IDataReader dr = db.ExecuteReader(dbCommand))
			{
				while(dr.Read())
				{
					list.Add(CreateSysConfigInfo(dr));
				}
			}
			
			return list;
		}

	
		/// <summary>
		/// Add
		/// </summary>
		/// <param name="sysConfigInfo"> entity</param>
		public static int Add(SysConfigInfo sysConfigInfo)
		{
				Database db = DatabaseFactory.CreateDatabase();
	
				DbCommand dbCommand = db.GetSqlStringCommand(TS_BAS_SYSCONFIG_INSERT);
				
				db.AddInParameter(dbCommand, "parameter_value",DbType.AnsiString,sysConfigInfo.Parameter_value); 
				db.AddInParameter(dbCommand, "parameter_description",DbType.AnsiString,DBConvert.ToDBValue(sysConfigInfo.Parameter_description)); 
				db.AddInParameter(dbCommand, "default_value",DbType.AnsiString,sysConfigInfo.Default_value); 
				db.AddInParameter(dbCommand, "changeable",DbType.Int32,sysConfigInfo.Changeable); 
				db.AddInParameter(dbCommand, "parameter_name",DbType.AnsiString,sysConfigInfo.Parameter_name); 
				
				return int.Parse("0"+db.ExecuteScalar(dbCommand));
		}
		
		/// <summary>
		/// Update
		/// </summary>
		/// <param name="sysConfigInfo"> utility</param>
		public static void Update(SysConfigInfo sysConfigInfo)
		{
			Database db = DatabaseFactory.CreateDatabase();
	
				DbCommand dbCommand = db.GetSqlStringCommand(TS_BAS_SYSCONFIG_UPDATE);
				
				db.AddInParameter(dbCommand, "parameter_value",DbType.AnsiString,sysConfigInfo.Parameter_value); 
				db.AddInParameter(dbCommand, "parameter_description",DbType.AnsiString,DBConvert.ToDBValue(sysConfigInfo.Parameter_description)); 
				db.AddInParameter(dbCommand, "default_value",DbType.AnsiString,sysConfigInfo.Default_value); 
				db.AddInParameter(dbCommand, "changeable",DbType.Int32,sysConfigInfo.Changeable); 
				db.AddInParameter(dbCommand, "parameter_name",DbType.AnsiString,sysConfigInfo.Parameter_name); 
				
				db.ExecuteNonQuery(dbCommand);
		}
		
		/// <summary>
		/// Delete
		/// </summary>
		/// <param name="parameter_name">TS_BAS_SysConfig Number</param>
		/// <returns></returns>
		public static void Delete(string parameter_name)
		{
			Database db = DatabaseFactory.CreateDatabase();
			DbCommand dbCommand = db.GetSqlStringCommand(TS_BAS_SYSCONFIG_DELETE);
			db.AddInParameter(dbCommand, "parameter_name", DbType.AnsiString,parameter_name);
			db.ExecuteNonQuery(dbCommand);
		}
		
	

	
		private static SysConfigInfo CreateSysConfigInfo(IDataReader rdr)
		{
			SysConfigInfo mysysConfigInfo = new SysConfigInfo();
			mysysConfigInfo.Parameter_value=rdr.GetString(0);
			mysysConfigInfo.Parameter_description=DBConvert.GetString(rdr,1);
			mysysConfigInfo.Default_value=rdr.GetString(2);
			mysysConfigInfo.Changeable=rdr.GetInt32(3);
			mysysConfigInfo.Parameter_name=rdr.GetString(4);

			return mysysConfigInfo;
		}
	}
}
